//
//  HRTabBar.swift
//  weiBo
//
//  Created by helong on 16/7/9.
//  Copyright © 2016年 helong. All rights reserved.
//

import UIKit

class HRTabBar: UITabBar {
    //MARK: - 中间按钮的闭包
    var addButtonClickClosure: (() -> ())?
    //MARK: -懒加载中间的按钮
    lazy var addButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Selected)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Selected)
        btn.addTarget(self, action: "addButtonClicked", forControlEvents: .TouchUpInside)
        //让按钮大小与图片一样大
        btn.sizeToFit()
        return btn
    }()

    //定义代理属性:注意使用  ==weak==
   weak var tarBarDelegate: addButtonDelegate?
    
    //MARK: - 构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)

//        let blurEffect = UIBlurEffect(style: .Light)
//        let blurEffectView = UIVisualEffectView(effect: blurEffect)
//        blurEffectView.frame = self.bounds
//        addSubview(blurEffectView)
//        blurEffectView.alpha = 0
        
//        self.backgroundColor = UIColor.whiteColor()
//        self.alpha = 0.95
//        self.translucent = false
        
        addSubview(addButton)
    }
    //MARK: - 中间按钮的点击事件
    /**
     中间按钮的点击事件
     */
    func addButtonClicked(){
        //通过闭包穿点点击事件
        addButtonClickClosure!()
        //代理传递
        tarBarDelegate?.addButtonClicked()
        
    }
    
//为了线程安全必须写的,这样写不能使用xib
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 布局子控件
    /**
     布局子控件
     */
    override func layoutSubviews() {
        super.layoutSubviews()
        /*不能这样写
        addButton.center.x = self.center.x
        addButton.center.y = self.center.y
        */
        //设置中间按钮的位置
        addButton.center.x = self.width * 0.5
        addButton.center.y = self.height * 0.5
        //得到tabBar按钮的宽度
        let width = self.frame.width / 5
        var index: Int = 0//计数器
        for values in subviews{
            if values .isKindOfClass(NSClassFromString("UITabBarButton")!){//如果子控件是UITabBarButton类
                //  设置按钮的宽度
                values.width = width
                values.x = width * CGFloat(index)
                if index == 1 {
                    index++
                }
                index++
            }
        }

         /*
            没有定义UIview分类的写法
            if values .isKindOfClass(NSClassFromString("UITabBarButton")!){//如果子控件是UITabBarButton类
                let originFrame = values.frame
                //设置新的frame
                let x = width * CGFloat(index)
                let y = originFrame.origin.y
                let w = width
                let h = originFrame.height
                
                let newFrame = CGRectMake(x, y, w, h)
                //因为中间有一个按钮,所以要做判断,到这个地方的时候跳一个按钮
                if index == 1{
                    index++
                }
               values.frame = newFrame
                index++
            }*/
        

    
    }
}

protocol addButtonDelegate: NSObjectProtocol{
    
    func addButtonClicked()
}
